var syllabusId,type,courseId,order,money,coursePageIds,noSid;
$(function(){
	getQueryStringByName();
});

function getQueryStringByName(){
	courseId = slf.getQueryStringByName("courseId");
	order = slf.getQueryStringByName("order");
	coursePageIds=slf.getQueryStringByName("coursePageIds");
	//判断是从主题预约过来还是体验预约过来
	if(coursePageIds==undefined||coursePageIds==null|coursePageIds==''||coursePageIds=="undefined"){
		//主题预约
		teamCourse()
	}else{
		//体验预约
		loadCourse()
	}
	if(slf.getQueryStringByName("noSid")==undefined){
		//从商城课包过来
	}else{
		//从预约过来
		noSid=slf.getQueryStringByName("noSid");//为1的话为大包id
	}
	
}
function loadCourse(){
	//获取课程包价格
	if(noSid==1){
		var param={
			schoolCoursePageId:'',
		}
	}else{
		var param={
			schoolCoursePageId:coursePageIds,
		}
	}
	slf.ajax({
		url:'/schoolCoursePage/getSchoolCoursePageMoney',
		async:false,
		param:param,
		success:function(data){
			console.log(data)
			console.log("获取价格")
			if(data.res==1&&data.obj.length>0){
				if(noSid==1){
					for(var i=0;i<data.obj.length;i++){
						if(data.obj[i].coursePageId==coursePageIds){
							if(data.obj[i].discountPrice!=null&&data.obj[i].discountPrice!=''&&data.obj[i].discountPrice!=undefined){
								money=data.obj[i].discountPrice;
								$("#price").text(data.obj[i].price+"立方币");
								$(".priceDiv").append('<span class="prices">折扣价：<span id="discountPrice">'+data.obj[i].discountPrice+'立方币</span></span>')
							}else{
								money=data.obj[i].price;
								$("#price").text(data.obj[i].price+"立方币");
							}
						}
					}
				}else{
					if(data.obj[0].isBuy=="Y"){
						//已购买
						$("#buyBtn").html('<span style="color:#10D634">已购买</span>')
					}else{
						//未购买
						$("#buyBtn").html('<button class="am-align-right am-margin-0 radius20 font14 buy_classbtn" style="margin-top:8px!important;min-width:0;background:#FFAB2A;">购买</button>')
					}
					if(data.obj[0].discountPrice!=null&&data.obj[0].discountPrice!=''&&data.obj[0].discountPrice!=undefined){
						money=data.obj[0].discountPrice;
						$("#price").text(data.obj[0].price+"立方币");
						$(".priceDiv").append('<span class="prices">折扣价：<span id="discountPrice">'+data.obj[0].discountPrice+'立方币</span></span>')
					}else{
						money=data.obj[0].price;
						$("#price").text(data.obj[0].price+"立方币");
					}
				}
				
			}
		}
	});
	//获取课表信息
	slf.ajax({
		url : "/system/getSchoolCourseUser",
		param : {courseId : courseId},
		type:'get',
		success : function(data){
			console.log(data)
			var course = data.obj;
			if(course.length>0){
				for(var i=0,b=data.obj.length;i<b;i++){
					$("#courseName").text(course[i].name);
					if(course[i].description==""||course[i].description==undefined||course[i].description==null){
						$("#description").html("暂无数据哦~看看其他的吧!");
					}else{
						$("#description").html(course[i].description);
					}
					
				  }
		    }else{
				$("#description").html("暂无数据哦~看看其他的吧!");
			}
		}
	});
}
function teamCourse(){
	if(slf.getQueryStringByName("nowTime")==undefined||slf.getQueryStringByName("nowTime")=='undefined'){
		var param={
			courseId : courseId,
			id:slf.getQueryStringByName("id"),
			seconds:null,
			flag:slf.getQueryStringByName("flag"),	
		}
	}else{
		var param={
			courseId : courseId,
			id:slf.getQueryStringByName("id"),
			seconds:slf.getQueryStringByName("nowTime"),
			flag:slf.getQueryStringByName("flag"),	
		}
	}
	slf.ajax({
		url : "/detailsTheme",
		param : param,
		success : function(data){
			console.log("主体预约返回值")
			console.log(data)
			var course = eval('('+ data.obj+')' );
			console.log(course)
			if(course.seconds=="可预约"){
				if(course.aloneCoursePages[0].discountPrice==undefined||course.aloneCoursePages[0].discountPrice==null||course.aloneCoursePages[0].discountPrice==""){
					var money=course.aloneCoursePages[0].price
				}else{
					var money=course.aloneCoursePages[0].discountPrice
				}
				$("#buyBtn").html('<button class="am-align-right am-margin-0 radius20 font14 orderBtn" onclick="orderBuy('+money+','+course.aloneCoursePages[0].coursePageId+')">预约</button>')
			}
			$("#courseName").text(course.aloneCoursePages[0].name);
			if(course.aloneCoursePages[0].description==""||course.aloneCoursePages[0].description==undefined||course.aloneCoursePages[0].description==null){
				$("#description").html("暂无数据哦~看看其他的吧!");
			}else{
				$("#description").html(course.aloneCoursePages[0].description);
			}
			
		}
	});
}
$("body").on('click', ".buy_classbtn", function() { //点击购买课程包
	$(".zcc").removeClass("am-hide")
	if(slf.checkSign()){
		//已登录
		var childId=slf.getSequenceChild();
		if(childId==undefined||childId==null||childId==''){
			$(".zcc").addClass("am-hide")
			var messPrompt="请先完善孩子信息，是否现在去完善？"
			$("#class_model .am-modal-bd").html(messPrompt)
			$("#class_model").modal({
				closeViaDimmer: false,
				onConfirm: function(options) {
					window.location.href = "personal/child_list.html"
		        },
			});
		}else{
			if(childId.state==0){//无期望上课时间
				console.log("无孩子期望上课时间")
				$(".zcc").addClass("am-hide")
				$("#class_model .am-modal-bd").html("您还未选择孩子期望上课时间段，是否现在去完善？")
				$("#class_model").modal({
					relatedTarget: this,
					closeViaDimmer: false,
					onConfirm: function(options) {
						window.location.href = "personal/add_child.html?userId="+childMess.userId+"&childrenId="+childMess.childrenId+""; //去其网上可时间  
					}
				})
			}else if(childId.state==1){//有期望上课时间
				console.log("有孩子期望上课时间")
				confirm(2,slf.childId);
			}
		}
	}else{
		//未登录
		$(".zcc").addClass("am-hide")
		var messPrompt="此功能仅对登录用户开放，是否需要登录？"
		$(".operationModalBody").html(messPrompt)
		$("#operationModal").modal({
			closeViaDimmer: false,
			onConfirm: function(options) {
				window.location.href = "login.html"
	        }
		})
	}
});
function orderBuy(money,courseId){
	$(".zcc").removeClass("am-hide")
	if(slf.checkSign()){
		//已登录
		var childId=slf.getSequenceChild();
		if(childId==undefined||childId==null||childId==''){
			$(".zcc").addClass("am-hide")
			var messPrompt="请先完善孩子信息！"
			$(".alert1").html(messPrompt)
			$("#alert1").modal({
				closeViaDimmer: false,
			})
		}else{
			if(childId.state==0){//无期望上课时间
				$(".zcc").addClass("am-hide")
				console.log("无孩子期望上课时间")
				$("#class_model .am-modal-bd").html("您还未选择孩子期望上课时间段，是否现在去完善？")
				$("#class_model").modal({
					relatedTarget: this,
					closeViaDimmer: false,
					onConfirm: function(options) {
						window.location.href = "personal/add_child.html?userId="+childMess.userId+"&childrenId="+childMess.childrenId+""; //去其网上可时间  
					}
				})
			}else if(childId.state==1){//有期望上课时间
				console.log("有孩子期望上课时间")
				$('#class_model .am-modal-hd').html("");
				$('#class_model .am-modal-bd').html("是否花费<span class='text-FF2A2A'>"+money+"</span>个立方币预约本课程?");
				$(".zcc").addClass("am-hide")
				$('#class_model').modal({
					relatedTarget: this,
					closeViaDimmer: false,
					onConfirm: function(options) {
						slf.ajax({
							url:'/getCourseThemePay',
							param:{
								id:slf.getQueryStringByName("id"),
								courseId:courseId,
							},
							success:function(data){
								console.log(data)
								console.log("判断是否购买")
								if(data.obj==0){
									//钱不够
									$("#buyMoneyModal").modal({
										relatedTarget: this,
										closeViaDimmer: false,
								        onConfirm: function(options) {
								        	window.location.href = "recharge.html"; //去充值页面
								        },
									});
								}else if(data.obj==1){
									//成功
									$("#alert_model .am-modal-bd").html("购买成功");
									$('#alert_model').modal("open");
									window.location.href = "subject_courseOrder.html";
								}else if(data.obj==2){
									$(".alert1").html("您购买的课程暂无相关信息，请稍后重试")
									$("#alert1").modal();
								}
							}
						});
					}
				});
			}
		}
	}else{
		//未登录
		$(".zcc").addClass("am-hide")
		var messPrompt="此功能仅对登录用户开放，是否需要登录？"
		$(".operationModalBody").html(messPrompt)
		$("#operationModal").modal({
			closeViaDimmer: false,
			onConfirm: function(options) {
				window.location.href = "login.html"
	        }
		})
	}
}
function goBuy(childrenId){
	console.log(coursePageIds+","+childrenId)
	console.log("获取主课程id")
		slf.ajax({
			url : "/myCoursePage/insertMyCoursePage",
			param : {
				courseId : coursePageIds,
				childrenId : childrenId
			},
			success : function(data){
				console.log("购买的接口")
				console.log(data)
				var course = data.obj;
				if(data.res != 1){
					$("#alert_model .am-modal-bd").html("<span>"+data.resMsg+"</span>");
					$('#alert_model').modal("open");
					clearFlag = self.setInterval("autoClose()", 1000); //每过一秒调用一次autoClose方法
				}else{
					console.log("购买成功")
				}
			}
		});
}
function confirm(x,childrenId){
	var html = "";
	$('#class_model .am-modal-hd').html("购买课程包");
	switch(x) {
	case 0: //在课程关系链中没有前置关系的情况
		html += "<ul class='am-avg-sm-1 am-text-left font14'>";
		html += "<li>您已经有进行中的系统学习类课程，但与即将购买的课程不冲突，请在确认自身上课时间重组后再购买</li>";
		html += "<li>是否花费<span class='text-FF2A2A'>1800</span>(原价2000)个立方币购买本课程包?<li>"
			html += "</ul>";
		$("#class_model .am-modal-bd").html(html);
		$('#class_model').modal({
			relatedTarget: this,
			closeViaDimmer: false,
			onConfirm: function(options) {
				$("#class_model").removeData('amui.modal');
				console.log("在课程关系链中没有前置关系的情况,购买课程结束");
			}
		});
		break;
	case 1: //课程包关系链中有前置关系的情况
		html += "<ul class='am-avg-sm-1 am-text-left font14'>";
		html += "<li>您已经有进行中的系统学习类课程，购买当前课程包后，会在您上完本课程包的所有课程后自动为您排班上课</li>";
		html += "<li>是否花费<span class='text-FF2A2A'>1800</span>(原价2000)个立方币购买本课程包?<li>"
			html += "</ul>";
		$("#class_model .am-modal-bd").html(html);
		$('#class_model').modal({
			relatedTarget: this,
			closeViaDimmer: false,
			onConfirm: function(options) {
				$("#class_model").removeData('amui.modal');
				console.log("课程包关系链中有前置关系的情况,购买课程结束");
				resmodel(1); //购买成功 失败
			}
		});
		break;
	case 2: //正常购买
		html += "<ul class='am-avg-sm-1 am-text-left font14'>";
		html += "<li>是否花费<span class='text-FF2A2A'>"+money+"</span>个立方币购买本课程包?<li>"
			html += "</ul>";
		$("#class_model .am-modal-bd").html(html);
		$(".zcc").addClass("am-hide")
		$('#class_model').modal({
			relatedTarget: this,
			closeViaDimmer: false,
			onConfirm: function(options) {
				$("#class_model").removeData('amui.modal');
				goBuy(childrenId);
			}
		});
		break;
	case 3: //有打折信息时的情况
		html += "<ul class='am-avg-sm-1 am-text-left font14'>";
		html += "<li>您当前距右参与优惠活动资格，是否花费<span class='text-FF2A2A'>1800</span>个立方币购买本课程包?<li>"
			html += "</ul>";
		$("#class_model .am-modal-bd").html(html);
		$('#class_model').modal({
			relatedTarget: this,
			closeViaDimmer: false,
			onConfirm: function(options) {
				$("#class_model").removeData('amui.modal');
				console.log("有打折信息时的情况,购买课程结束")
			}
		});
		break;
	}
}
//购买成功 购买失败
function resmodel(s) {
	if(s == 1) {
		$("#alert_model .am-modal-bd").html("购买成功！");
		count = 3;
		$('#alert_model').modal("open");
		clearFlag = self.setInterval("autoClose()", 1000); //每过一秒调用一次autoClose方法
	} else {
		$('#child_model .am-modal-hd').html("购买失败!");
		var html = "";
		html += "<ul class='am-avg-sm-1 am-text-center font14'>";
		/*html += "<li>购买失败！<li>"*/
		html += "<li>余额不足，是否立即充值?<li>"
		html += "</ul>";
		$("#child_model .am-modal-bd").html(html);
		$('#child_model').modal({
			relatedTarget: this,
			closeViaDimmer: false,
			onConfirm: function(options) {
				$("#child_model").removeData('amui.modal');
				location.href = "recharge.html"; //去充值页面
			}
		});
	}
}